////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.index;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.IndexGroup;
import cc.telecomdigital.tdstock.adapter.CommonViewHolder;
import cc.telecomdigital.tdstock.adapter.ForeignExchangeRateOptionsAdapter;

/**
 * 
 * @author haibo
 * 
 *  for top bar button  named "options"
 *
 */
public class IndexOptionsListActivity extends BaseActivity
{
	protected static final String	TAG			= IndexOptionsListActivity.class.getSimpleName();
	private static final long	START_ID		= 90000l;

	private TextView[]			_txt;
	private Drawable[]			_sign;
	private ListView			_listView;
	private Button				_toolBtnReturn;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_options_list);
		
		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");
				// BMP Mode by ouyangzhihui 2011-10-13
				Class<? extends Activity> _cls = IndexActivity.class;
				if(((ITDLApplication)getApplicationContext()).getIsBMPMode())
				{
					_cls = BMPIndexActivity.class;
				}
				Intent intent = new Intent(getApplicationContext(), _cls);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				
				SwitchForwardActivity(_cls, intent);
			}
		});

		resetTitle();

		_listView = (ListView) findViewById(R.id.options_list);
		final String[] txtRes = this.getResources().getStringArray(R.array.index_option_list);
		int listSize = txtRes.length;
		_txt = new TextView[listSize];
		_sign = new Drawable[listSize];
		for (int index = 0; index < listSize; index++)
		{
			TextView title = new TextView(this);
			title.setText(txtRes[index]);
			_txt[index] = title;
			_sign[index] = this.getResources().getDrawable(
					R.drawable.go_next_icon);
		}

		List<CommonViewHolder> list = new ArrayList<CommonViewHolder>();
		for (int index = 0; index < listSize; index++)
		{
			long id = START_ID + index;
			CommonViewHolder viewHolder = new CommonViewHolder();
			viewHolder.id = id;
			viewHolder.text = _txt[index];
			viewHolder.sign = new ImageView(this);
			viewHolder.sign.setImageDrawable(_sign[index]);

			list.add(viewHolder);

		}
		_messageHandler = null; //this activity no need it.
		_listView.setAdapter(new ForeignExchangeRateOptionsAdapter(LayoutInflater.from(this), list));
		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				if (position != -1 && position != 1)
				{
					IndexOptionContentActivity.iTypeId = position;
					IndexOptionContentActivity._title = txtRes[position];
					
					Intent intent = new Intent(getApplicationContext(), IndexOptionContentActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
					SwitchForwardActivity(IndexOptionContentActivity.class, intent);
				}
				else if (position == 1)
				{
					Class<? extends Activity> copCls = IndexCOPListActivity.class;
					if(((ITDLApplication)getApplicationContext()).getIsBMPMode())
					{
						copCls = BMPIndexCOPListActivity.class;
					}

					((ITDLApplication) getApplicationContext()).putString(CommonDefn.TOP_BAR_TITLE, txtRes[position]);

					Intent intent = new Intent(getApplicationContext(), copCls);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
					SwitchForwardActivity(copCls, intent);
				}
			}
		});
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		_listView.invalidateViews();
	}

	private void resetTitle()
	{
		if (((ITDLApplication) getApplicationContext()).getDataMode() == CommonDefn.DataMode.Delay)
		{
			_toolBtnReturn.setTextSize(12);
			_toolBtnReturn.setText(getResources().getString(R.string.index)
					+ "("
					+ getResources().getString(
							R.string.delay_mode_title_tips) + ")");
		}
		else
		{
			_toolBtnReturn.setText(getResources().getString(R.string.index));
		}
		_toolBtnReturn.postInvalidate();
	}
	
	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return IndexGroup.GetActivityGroupHostControl();
	}
}
